<template>
  <div class="backup-list">
    <el-card class="list-card" shadow="hover">
      <div slot="header" class="card-header">
        <div class="header-left">
          <i class="el-icon-document-copy"></i>
          <span>备份列表</span>
          <el-tooltip content="展示数据库的备份记录及下载" placement="right">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="operation-bar">
        <div class="left-operations">
          <el-button-group>
            <el-button size="small" icon="el-icon-refresh" @click="refreshList">刷新</el-button>
          </el-button-group>
        </div>
        <div class="search-box">
          <el-input
            v-model="searchQuery"
            placeholder="搜索数据库名称"
            prefix-icon="el-icon-search"
            clearable
            @clear="handleSearch"
            @keyup.enter.native="handleSearch">
          </el-input>
        </div>
      </div>

      <!-- 备份列表 -->
      <el-table
        :data="filteredTableData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        stripe>
        <el-table-column prop="databaseName" label="数据库名称" min-width="200">
          <template slot-scope="scope">
            <el-tooltip :content="scope.row.databaseName" placement="top">
              <span class="database-name">{{ scope.row.databaseName }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="databaseType" label="数据库类型" min-width="120">
          <template slot-scope="scope">
            <el-tag size="small" effect="plain">{{ scope.row.databaseType }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="backupSize" label="备份大小" width="120">
          <template slot-scope="scope">
            <span class="size-text">{{ scope.row.backupSize }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="backupMode" label="备份模式" width="120">
          <template slot-scope="scope">
            <el-tag
              size="small"
              :type="scope.row.backupMode === '自动备份' ? 'success' : 'primary'">
              {{ scope.row.backupMode }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="backupTime" label="备份时间" min-width="180"></el-table-column>
        <el-table-column prop="status" label="备份集状态" width="120">
          <template slot-scope="scope">
            <el-tag
              size="small"
              :type="getStatusType(scope.row.status)"
              effect="dark">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleDownload(scope.row)"
              :disabled="scope.row.status !== 'OK'">
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 空状态 -->
      <div v-if="!loading && tableData.length === 0" class="empty-state">
        <img src="/placeholder.svg?height=120&width=120" alt="No Data" />
        <p>暂无备份数据</p>
      </div>

      <!-- 分页器 -->
      <div v-if="tableData.length > 0" class="pagination-container">
        <span class="total-count">共 {{ total }} 条数据</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 下载进度对话框 -->
    <el-dialog
      title="下载进度"
      :visible.sync="downloadDialogVisible"
      :close-on-click-modal="false"
      :show-close="false"
      width="400px">
      <div class="download-progress">
        <el-progress
          :percentage="downloadProgress"
          :status="downloadProgress === 100 ? 'success' : ''"
          :stroke-width="18">
        </el-progress>
        <p class="progress-text">{{ downloadProgress === 100 ? '下载完成' : '正在下载备份文件...' }}</p>
      </div>
      <div slot="footer" class="dialog-footer" v-if="downloadProgress === 100">
        <el-button type="primary" @click="downloadDialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'BackupList',
  data() {
    return {
      // 搜索相关
      searchQuery: '',
      loading: false,

      // 表格数据
      tableData: [
        {
          databaseName: 'test1234.test002.test003',
          databaseType: 'SQLServer',
          backupSize: '14.07M',
          backupMode: '自动备份',
          backupTime: '2024-11-25 22:53:41',
          status: 'OK'
        }
      ],

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 1,

      // 下载相关
      downloadDialogVisible: false,
      downloadProgress: 0,
      downloadTimer: null
    }
  },
  computed: {
    filteredTableData() {
      if (!this.searchQuery) {
        return this.tableData
      }
      const query = this.searchQuery.toLowerCase()
      return this.tableData.filter(data => {
        return data.databaseName.toLowerCase().includes(query)
      })
    }
  },
  methods: {
    // 搜索相关方法
    handleSearch() {
      this.currentPage = 1
      this.getList()
    },

    // 分页相关方法
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 获取状态类型
    getStatusType(status) {
      const types = {
        'OK': 'success',
        'ERROR': 'danger',
        'PROCESSING': 'warning'
      }
      return types[status] || 'info'
    },

    // 下载相关方法
    handleDownload(row) {
      this.downloadDialogVisible = true
      this.downloadProgress = 0

      // 模拟下载进度
      this.downloadTimer = setInterval(() => {
        if (this.downloadProgress < 100) {
          this.downloadProgress += 10
        } else {
          clearInterval(this.downloadTimer)
          // 模拟文件下载
          setTimeout(() => {
            this.$message.success('备份文件下载成功')
          }, 500)
        }
      }, 300)
    },

    // 获取列表数据
    getList() {
      this.loading = true
      // 模拟API调用
      setTimeout(() => {
        this.loading = false
      }, 800)
    },

    // 刷新列表
    refreshList() {
      this.getList()
    }
  },
  beforeDestroy() {
    if (this.downloadTimer) {
      clearInterval(this.downloadTimer)
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.backup-list {
  padding: 0px;
}

.list-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-left i {
  font-size: 18px;
}

.header-left .el-icon-question {
  font-size: 14px;
  color: #909399;
  cursor: help;
}

.operation-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.left-operations {
  display: flex;
  gap: 10px;
}

.search-box {
  width: 300px;
}

.database-name {
  font-family: monospace;
  color: #409EFF;
}

.size-text {
  font-family: monospace;
  color: #67C23A;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #909399;
}

.empty-state img {
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.total-count {
  color: #606266;
  font-size: 14px;
}

/* 表格样式优化 */
.el-table {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.el-table th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 下载进度对话框样式 */
.download-progress {
  padding: 20px;
  text-align: center;
}

.progress-text {
  margin-top: 15px;
  color: #606266;
  font-size: 14px;
}

/* 标签样式 */
.el-tag {
  border-radius: 4px;
  padding: 0 8px;
  height: 24px;
  line-height: 22px;
}

/* 加载动画样式 */
.el-loading-spinner .el-icon-loading {
  font-size: 30px;
  color: #409EFF;
}

.el-loading-spinner .el-loading-text {
  color: #409EFF;
  margin: 3px 0;
  font-size: 14px;
}
</style>
